package com.zxt.utils;

import com.zxt.mapper.DataMapper;

import javax.sql.DataSource;
import java.sql.*;
import java.util.List;

/**
 * Improve check and insert Data Methods.
 * Created by XiaoQiAng on 2015/11/6.
 */
public class JDBCTemplate {
    private DataSource dataSource = null;
    private Connection connection = null;
    private PreparedStatement pStatement = null;
    private ResultSet rs = null;

    public JDBCTemplate(DataSource dataSource){
        this.dataSource = dataSource;
    }

    /**
     * Use Sql and args to check Data From Mysql
     * return Result set, Mapped by ordered rowmapper
     * @param sql
     * @param obj
     * @param dataMapper
     * @return
     */
    public List checkData(String sql, Object[] obj, DataMapper dataMapper) {
        if (connection == null) {
            try {
                connection = dataSource.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            pStatement = connection.prepareStatement(sql);
            setPstmtParemeter(obj);
            return dataMapper.rowMapper(pStatement.executeQuery());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        return null;
    }

    /**
     * Use Sql and args to insert Data to mysql
     * @param sql
     * @param obj
     * @return
     */
    public boolean insertData(String sql, Object[] obj) {
        if (connection == null) {
            try {
                 connection = dataSource.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            pStatement = connection.prepareStatement(sql);
            setPstmtParemeter(obj);
            if (pStatement.executeUpdate() == 1 ) {
                return true;
            }
            return false;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
        return false;
    }

    /**
     * This method used to set args for prepareStatement.
     * @param obj
     * @throws Exception
     */
    public void setPstmtParemeter(Object[] obj) throws Exception{
        ParameterMetaData metaData = pStatement.getParameterMetaData();
        int count = metaData.getParameterCount();

        for (int i = 1; i <= count; i++) {
            pStatement.setObject(i, obj[i - 1]);
        }
    }

    /**
     * release Source.
     */
    public void release() {
        try {
            if (rs != null && !rs.isClosed()) {
                rs.close();
                rs = null;
            }
            if (pStatement != null && !pStatement.isClosed()) {
                pStatement.close();
                pStatement = null;
            }
            if (connection != null && !connection.isClosed()) {
                connection.close();
                connection = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
